<?php

namespace app\common\job;

use think\facade\Log;
use think\queue\Job;

/**
 * 模板短信发送
 * Class TemplateSms
 * @package app\common\job
 */
class TemplateSms
{
    /**
     * fire是消息队列默认调用的方法
     * @param Job $job 当前的任务对象
     * @param array|mixed $data 发布任务时自定义的数据
     */
    public function fire(Job $job, $data)
    {
        $template_id = $data['template_id'] ?? '';
        $sign_name = $data['sign_name'] ?? '';
        $params = $data['params'] ?? [];
        $phone = $data['phone'] ?? '';

        $response = $this->getTemplateSms()->send($template_id, $sign_name, $params, $phone);

        if (!$response->isSuccess()) {
            Log::warning('模板短信发送失败');
        }

        $job->delete();
    }

    private function getTemplateSms(): \ShidianUtil\Api\Sms\TemplateSms
    {
        return app(\ShidianUtil\Api\Sms\TemplateSms::class);
    }
}
